
# =============================================================
# File: geography_map.R
# Purpose: Plots study counts by country on map
# Date: 19 July 2019
# Data: ./Data_maps.xlsx
#
# Techincal disclaimer:
# All analyses in R version 3.5.0 (2018-04-23) using RStudio Version 1.1.453 on Windows 10, 64 bit
#	Machine: Intel(R) Core(TM) i7-4650U CPU @ 1.70Ghz 2.30Ghz with 8GB RAM
# =============================================================

# Clear memory
rm(list=ls())

# Load library
library(foreign)
library(plyr)
library(ggplot2)
library(rgeos)
library(maptools)
library(maps)
library(readxl)
library(RColorBrewer)


# Load data
data <- read_excel("./Data_maps.xlsx")

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


# =============================================================
# Plot begins here
# =============================================================

# Link to regional country code
# https://en.wikipedia.org/wiki/UN_M.49

data(wrld_simpl)
# Restrict map to Africa, Americas, and Asia
# region_code=19: LAC
# region_code=2: SSA
# region_code=34: Southern Asia
# region_code=35: Soutth-eastern Asia


# Exclude North America
wrld.map <- wrld_simpl[wrld_simpl@data$SUBREGION!="21",]

# Exclude North Africa
#wrld.map <- wrld_simpl[wrld_simpl@data$SUBREGION!="15",]


wrld.map <- wrld.map[wrld.map@data$REGION=="2" | wrld.map@data$SUBREGION=="35" | wrld.map@data$SUBREGION=="34" | wrld.map@data$REGION=="19", ] 
wrld.map@data$id <- wrld.map@data$NAME
wrld.map <- wrld.map[wrld.map@data$AREA>2000,]


wrld.plot <- fortify(wrld.map, region="id")
wrld.plot$id[wrld.plot$id=="United Republic of Tanzania"] <- "Tanzania"

# Trick to ensure Lesotho is colored white
wrld.plot.les <- wrld.plot[wrld.plot$id=="Lesotho",]

p <- ggplot(data=wrld.plot) +
     geom_map(data=wrld.plot, map=wrld.plot, aes(map_id=id, x=long, y=lat), fill="white", color="#7f7f7f", size=0.25) +
     geom_map(data=data, map=wrld.plot, aes(map_id=country, fill=factor(count)),color="#7f7f7f",size=0.25) +
     geom_map(data=wrld.plot.les, map=wrld.plot.les, aes(map_id=id, x=long, y=lat),fill="white",color="#7f7f7f", size=0.25) +
    scale_fill_brewer(name="Study count")+  
  #scale_fill_manual(values=colorcode,name="Study count") +
     theme(plot.background = element_rect(fill = "transparent", colour = NA),
        panel.border = element_blank(),
        #panel.background = element_rect(fill = "transparent", colour = NA),
        #panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "bottom",
        axis.title = element_blank()) +
        coord_equal() + 
        expand_limits(y=c(-60,20))
p
       
ggsave(p,filename="./study_map.png",width=10, height=4)		  
	
# =============================================================

